package com.lantosec.hmrp.modules.supplier.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.core.exception.AppException;
import com.lantosec.hmrp.core.shiro.TokenUtil;
import com.lantosec.hmrp.modules.supplier.entity.SupplierEntry;
import com.lantosec.hmrp.modules.supplier.mapper.SupplierEntryMapper;
import com.lantosec.hmrp.modules.supplier.service.ISupplierEntryService;
import com.lantosec.hmrp.modules.supplier.vo.SupplierEntryVO;
import com.lantosec.hmrp.modules.system.entity.SysDictionaries;
import com.lantosec.hmrp.modules.system.service.ISysDictionariesService;
import com.lantosec.hmrp.modules.workflow.service.IWorkflowBusinessService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 供应商准入信息 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-02-27
 */
@Service
@Primary
@Log4j2
public class SupplierEntryServiceImpl extends ServiceImpl<SupplierEntryMapper, SupplierEntry> implements ISupplierEntryService {

    @Autowired
    private IWorkflowBusinessService workflowBusinessService;
    @Autowired
    private ISysDictionariesService sysDictionariesService;

    @Override
    public String querySupplierType(String supplierCode,String entryState) {
        SupplierEntry supplierEntry = baseMapper.selectOne(new QueryWrapper<SupplierEntry>().select(SupplierEntry.ColName.supplier_type.toString())
                            .eq(SupplierEntry.ColName.supplier_code.toString(),supplierCode).eq(SupplierEntry.ColName.entry_state.toString(),entryState));
        String supplierType = supplierEntry.getSupplierType();
        return supplierType;
    }

    @Override
    public void updateSupplierEntry(List<SupplierEntryVO> voList) {
        //获得redis中的用户名和id
        String currentUserId = TokenUtil.getUserId();
        String name = TokenUtil.getName();
        String definitionKey = this.getSupplierEntryProcessKey();
        voList.forEach(par -> {
            //有效期结束日期validity_end_date为申请时间+准入有效期月份
            //TODO 准入有效期月份参数无
            par.setValidityEndDate("2021-12-31");
            par.setEntryState(2);
            par.setEntryTime(new Date());
            par.setApplyUserId(currentUserId);
            par.setApplyUser(name);
            this.updateById(par.convertToEntity());

            Boolean flag = workflowBusinessService.startProcess(currentUserId, definitionKey, par);
        });
    }

    @Override
    public String getSupplierEntryProcessKey(){
        SysDictionaries sysDictionaries = sysDictionariesService.getById("014001");
        if(Objects.isNull(sysDictionaries)){
            throw new AppException("未找到对应的流程定义Key");
        }
        return sysDictionaries.getDictionariesShowName();
    }

}
